package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.TmsFragileStickerHistory;

/**
 * @author zhangxueliang
 * 根据会员id载入商户信息
 */
public class CarLoadMemberInfoAjaxAction extends TmsAjaxAction{
	private Logger logger = Logger.getLogger(CarLoadMemberInfoAjaxAction.class);
	private static final String MEMBERID = "memberId";

	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		controller.begin();
		String memberId = request.getParameter(MEMBERID);
		
		TmsFragileStickerHistory tmsFragileStickerHistory = TmsFragileStickerHistory.getInstance();
		Car car = Car.getInstance();
		Member member = Member.getInstance();
		member = member.findCarScoreByMemberId(memberId);		
		StringBuffer str = new StringBuffer();
		str.append("");
		if(member!=null)
		{
			String saveFlag = "0";
			String isAutoPayForRegister = Registry.getConfigFromDb(TmsConstants.ISAUTOPAYFORREGISTER);			
			if("3".equals(isAutoPayForRegister)){
				int checkPrice = Integer.valueOf(Registry.getConfigFromDb(TmsConstants.CHECKPRICE));
				int transferPrice = Integer.valueOf(Registry.getConfigFromDb(TmsConstants.TRANSFERPRICE));
				int virtualMargin = Integer.valueOf(member.getVirtualMargin());
				if(virtualMargin < (checkPrice + transferPrice)){
					saveFlag = "1";
				}
			}
			str.append(member.getContactOwner());
			str.append("?"+member.getContactPhone());
			str.append("?"+member.getFragileStickerFreePort());			
			str.append("?"+member.getPassword());
			str.append("?"+member.getUsedCarPort());
			str.append("?"+member.getCarPort());
			str.append("?"+tmsFragileStickerHistory.getFragileStickerByMemberId(memberId));
			str.append("?"+member.getAnnualTransactionVolume());
			str.append("?"+car.getAlreadyTransactionVolumeByMemberId(memberId));
			str.append("?"+member.getVirtualMargin());
			str.append("?"+saveFlag);
		}else{
			str.append("");
			str.append("?"+"");	
			str.append("?"+"0");			
			str.append("?"+"");
			str.append("?"+"0");
			str.append("?"+"0");
			str.append("?"+"0");
			str.append("?"+"0");
			str.append("?"+"0");
			str.append("?"+"0");
			str.append("?"+"0");
		}

		String responseText =str.toString();
		controller.commit();
		responseValues(response, responseText, TEXT);
		logger.debug(this.getClass().getName() + "#doAction was completed.");		
	}

}
